package com.mibao.activity;

import java.util.List;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
//import android.view.View;
//import android.view.View.OnClickListener;
//import android.widget.Button;
import android.widget.GridView;
import android.widget.Toast;

import com.learnncode.mediachooser.MediaChooser;
//import com.learnncode.mediachooser.activity.BucketHomeFragmentActivity;
import com.learnncode.mediachooser.activity.HomeAddPicFragmentActivity;
import com.mibao.adapter.MediaGridViewAdapter;
import com.mipai.R;

public class MainPictureActivity extends Activity {

//	Button folderViewButton;                       
//	Button PicViewButton;
	GridView gridView;
	MediaGridViewAdapter adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.picture_activity_main);
//		folderViewButton = (Button)findViewById(R.id.folderButton);
//		PicViewButton = (Button)findViewById(R.id.fileButton);
//		
		gridView = (GridView)findViewById(R.id.gridView);
//		folderViewButton.setOnClickListener(clickListener);
//		PicViewButton.setOnClickListener(clickListener);

//		IntentFilter videoIntentFilter = new IntentFilter(MediaChooser.VIDEO_SELECTED_ACTION_FROM_MEDIA_CHOOSER);
//		registerReceiver(videoBroadcastReceiver, videoIntentFilter);

		IntentFilter imageIntentFilter = new IntentFilter(MediaChooser.IMAGE_SELECTED_ACTION_FROM_MEDIA_CHOOSER);
		registerReceiver(imageBroadcastReceiver, imageIntentFilter);

	}
	@Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        getMenuInflater().inflate(R.layout.actionbar_menu, menu);  
        return true;  
    }  
    @Override
    public boolean onOptionsItemSelected(MenuItem item){
    	switch(item.getItemId()){
    	case R.id.addPicButton:       //添加图片按钮
    		Intent intent = new Intent(MainPictureActivity.this, HomeAddPicFragmentActivity.class);
			startActivity(intent);
    	}
    	return true;
    }
//	OnClickListener clickListener = new OnClickListener() {
//
//		@Override
//		public void onClick(View view) {
//			if(view == PicViewButton){     //图片浏览方式
//				Intent intent = new Intent(MainPictureActivity.this, HomeFragmentActivity.class);
//				startActivity(intent);
//			}
//			else {                         //目录浏览方式
//				MediaChooser.setSelectionLimit(20);   
//				Intent intent = new Intent(MainPictureActivity.this, BucketHomeFragmentActivity.class);
//				startActivity(intent);
//			}
//		}
//	};

//	BroadcastReceiver videoBroadcastReceiver = new BroadcastReceiver() {
//
//		@Override
//		public void onReceive(Context context, Intent intent) {
//
//			Toast.makeText(MainPictureActivity.this, "正在加密视频", Toast.LENGTH_SHORT).show();
//			Toast.makeText(MainPictureActivity.this, "已加密 :" + intent.getStringArrayListExtra("list").size() + "个视频", Toast.LENGTH_SHORT).show();
//			setAdapter(intent.getStringArrayListExtra("list"));
//		}
//	};


	BroadcastReceiver imageBroadcastReceiver = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Toast.makeText(MainPictureActivity.this, "正在加密图片", Toast.LENGTH_SHORT).show();
			Toast.makeText(MainPictureActivity.this, "已加密 :" + intent.getStringArrayListExtra("list").size()+ "个图片", Toast.LENGTH_SHORT).show();
			setAdapter(intent.getStringArrayListExtra("list"));
		}
	};

	@Override
	protected void onDestroy() {
		unregisterReceiver(imageBroadcastReceiver);
//		unregisterReceiver(videoBroadcastReceiver);
		super.onDestroy();
	}

	private void setAdapter( List<String> filePathList) {
		if(adapter == null){
			adapter = new MediaGridViewAdapter(MainPictureActivity.this, 0, filePathList);
			gridView.setAdapter(adapter);
		}else{
			adapter.addAll(filePathList);
			adapter.notifyDataSetChanged();
		}
	}
}
